import 'package:flutter_trip/model/travel_model.dart';
import 'package:http/http.dart' as http;
import 'dart:convert' as convert;

import '../model/travel_tab_model.dart';

var Params = {
  "districtId": -1,
  "groupChannelCode": "tourphoto_global1",
  "type": null,
  "lat": -180,
  "lon": -180,
  "locatedDistrictId": 2,
  "pagePara": {
    "pageIndex": 1,
    "pageSize": 10,
    "sortType": 9,
    "sortDirection": 0
  },
  "imageCutType": 1,
  "head": {
    "cid": "09031014111431397988",
    "ctok": "",
    "cver": "1.0",
    "lang": "01",
    "sid": "8888",
    "syscode": "09",
    "auth": null,
    "extension": [{"name": "protocal", "value": "https"}]
  },
  "contentType": "json"
};

class TravelTabDao {
  static Future<TravelTabModel> fetch() async {
    final response = await http.get(Uri.parse(
        'https://gitee.com/jsjxsy/flutter_trip/raw/master/json/travel_page.json'));
    if (response.statusCode == 200) {
      print("response: ${response.body}");
      convert.Utf8Decoder utf8decoder = const convert.Utf8Decoder();
      var result = convert.json.decode(utf8decoder.convert(response.bodyBytes));
      return TravelTabModel.fromJson(result);
    } else {
      throw Exception('Failed to load search json');
    }
  }



  static Future<TravelModel> fetchTab(String url, String groupChannelCode,
      int pageIndex, int pageSize) async {
    Map paramsMap = Params['pagePara'] as Map;
    paramsMap['pageIndex'] = pageIndex;
    paramsMap['pageSize'] = pageSize;
    Params['groupChannelCode'] = groupChannelCode;
    final response =  await http.post(Uri.parse(url), body: convert.jsonEncode(Params));
    if (response.statusCode == 200) {
      print("response: ${response.body}");
      convert.Utf8Decoder utf8decoder = const convert.Utf8Decoder();
      var result = convert.json.decode(utf8decoder.convert(response.bodyBytes));
      return TravelModel.fromJson(result);
    } else {
      throw Exception('Failed to load search json');
    }
  }
}